<!--

-->
<template>
    <vxe-modal v-model="visible" :position="{top: '60px'}" :title="title" destroy-on-close  :min-width="layout.modalMinWidth(600)" resize show-footer show-maximize :width="layout.modalWidth(600)" :height="layout.modalHeight()" @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="120px">
            <el-form-item :style="layout.col1" label="上级区域" prop="pCode">
                <el-tree-select v-model="state.model.pCode" @change="onChange" :check-strictly="true" :data="state.tree" :props="{label: 'name', children: 'children'}" :render-after-expand="false" style="width: 100%;" value-key="code"/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="名称" prop="name">
                <el-input v-model="state.model.name"/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="编号" prop="code">
                <el-input v-model="state.model.code"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, watch, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {
        "pCode": null,
        "areaChar": null,
        "zipCode": null,
        "level": 1,
        "isOpen": 0,
        "name": null,
        "code": null
    }
}

// 校验规则
const rules = computed(() => {
    return {
        name: [{required: true, message: "请输入名称"}],
        code: [{required: true, message: "请输入编号"}],
    }
})

function onChange(v) {
    const item = state.tree.treeFind(item => item.code === v);
    if (item) {
        state.model.level = (item.level || 1) + 1;
    }
}


// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    if (state.options) {
        state.model = {...state.model, ...state.options}
    }
    // 初始化数据
    const {success, data} = await api.GetAreaTree();
    if (!success) return 0;
    state.tree = data;

}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {...state.model}
    const {success, data} = await api[state.options.code ? 'UpdateAreaInfo' : 'AddAreaInfo'](body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>

<style lang="scss" scoped>
.EditForm {
}
</style>
